﻿using ProjectFootball.MVC.Foundation.Repositories;
using ProjectFootball.MVC.Models.EntityFramework.Entities;
using System;
using System.Linq;

namespace ProjectFootball.MVC.Models.RepositoryProviders
{
    public static class CampaignRepositoryExtensions
    {
        public static Campaign GetCampaignByDate(this IDataRepository<Campaign> campaignRepository, Guid competitionGuid, DateTime viewDate)
        {
            var campaigns = from c in campaignRepository
                            where c.CompetitionGuid == competitionGuid
                            && c.IsApproved
                            && c.StartDate <= viewDate
                            && c.EndDate >= viewDate
                            select c;

            return campaigns.Count() == 1 ? campaigns.Single() : null;
        }
    }
}